<?php

require_once(dirname(dirname(dirname(__FILE__))).'/includes.php');
require_once('lib.php');
require_once(dirname(dirname(__FILE__))."/common.php");

if (!$USER->ident || $USER->ident == 0){
	error('You must be logged in to access this page.');
}

$method = required_param('method', PARAM_ALPHA);

switch ($method){
	case 'getskillsforcategory':
		$scId = required_param('scid', PARAM_INT);
		$usId = optional_param('usid', 0, PARAM_INT);
		$response = getSkillsForCategory($scId, $usId);
		break;
	case 'getfile':
		$fileId = required_param('fileid', PARAM_INT);
		$usId = optional_param('usid', 0, PARAM_INT);
		$response = getFile($fileId,$usId);
		break;
	case 'addskill':
		$skillId = required_param('skillid', PARAM_INT);
		$skillWant = required_param('skillwant', PARAM_ALPHA);
		$skillDateD = optional_param('dateday',0, PARAM_INT);
		$skillDateM = optional_param('datemonth',0, PARAM_INT);
		$skillDateY = optional_param('dateyear',0, PARAM_INT);
		$skillLoc = optional_param('skilllocation','', PARAM_NOTAGS);
		$skillNotes = required_param('skillnotes', PARAM_NOTAGS);
		$skillEvidence = required_param('evidence', PARAM_NOTAGS);
		if ($skillDateM != 0 && $skillDateD != 0 && $skillDateY != 0){
			$skillDate = mktime(0, 0, 0, $skillDateM, $skillDateD, $skillDateY);
		} else {
			$skillDate = 0;
		}
		$response = addSkill($skillId,$skillDate,$skillLoc,$skillNotes,$skillEvidence,$skillWant);
		break;
	case 'removeskill':
		$skillId = required_param('skillid', PARAM_INT);
		$response = removeSkill($skillId);
		break;
	case 'getuserskill':
		$usId = required_param('usid', PARAM_INT);
		$response = getUserSkill($usId);
		break;
	case 'getcategories':
		$usId = optional_param('usid',0,PARAM_INT);
		$response = getCategories($usId);
		break;
	case 'getfiles':
		$usId = optional_param('usid',0,PARAM_INT);
		$response = getFiles($usId);
		break;
	case 'editskill':
		$usId = required_param('usid', PARAM_INT);
		$skillId = required_param('skillid', PARAM_INT);
		$skillWant = required_param('skillwant', PARAM_ALPHA);
		$skillDateD = optional_param('dateday',0, PARAM_INT);
		$skillDateM = optional_param('datemonth',0, PARAM_INT);
		$skillDateY = optional_param('dateyear',0, PARAM_INT);;
		$skillLoc = optional_param('skilllocation','', PARAM_NOTAGS);
		$skillNotes = required_param('skillnotes', PARAM_NOTAGS);
		$skillEvidence = required_param('evidence', PARAM_NOTAGS);
		if ($skillDateM != 0 && $skillDateD != 0 && $skillDateY != 0){
			$skillDate = mktime(0, 0, 0, $skillDateM, $skillDateD, $skillDateY);
		} else {
			$skillDate = 0;
		}
		$response = editSkill($usId,$skillId,$skillDate,$skillLoc,$skillNotes,$skillEvidence,$skillWant);
}

// set header to be XML
header('Content-Type: text/xml;');
$str = '<?xml version="1.0" encoding="UTF-8"?>';
$str .= '<response>';
if ($response) {
	$str .= responseToXML($response);
}
$str .= '</response>';

echo $str;

?>